home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_562 / intuisup / editor / source.lzh / subs.c < prev    next >
C/C++ Source or Header  |  1991-10-20  |  8KB  |  309 lines

  1.         /*************************************
  2.          *                                   *
  3.          *             Editor v1.0           *
  4.          *   by Torsten Jürgeleit in 07/91   *
  5.          *                                   *
  6.          *          Subroutines part         *
  7.          *                                   *
  8.          *************************************/
  9.  
  10.     /* Includes */
  11.  
  12. #include "includes.h"
  13. #include "defines.h"
  14. #include "imports.h"
  15. #include "protos.h"
  16.  
  17.     /* Draw box in given window */
  18.  
  19.    VOID
  20. draw_box(struct Window  *win, struct Box  *box)
  21. {
  22.    struct RastPort  *rp = win->RPort;
  23.  
  24.    SetDrMd(rp, (LONG)COMPLEMENT);
  25.    Move(rp, (LONG)box->bo_X1, (LONG)box->bo_Y1);
  26.    Draw(rp, (LONG)box->bo_X2, (LONG)box->bo_Y1);
  27.    Draw(rp, (LONG)box->bo_X2, (LONG)box->bo_Y2);
  28.    Draw(rp, (LONG)box->bo_X1, (LONG)box->bo_Y2);
  29.    Draw(rp, (LONG)box->bo_X1, (LONG)box->bo_Y1);
  30. }
  31.     /* Draw box in given window with border of 1 pixel */
  32.  
  33.    VOID
  34. draw_box_with_border(struct Window  *win, struct Box  *box)
  35. {
  36.    struct RastPort  *rp = win->RPort;
  37.  
  38.    SetDrMd(rp, (LONG)COMPLEMENT);
  39.    Move(rp, (LONG)(box->bo_X1 - 1), (LONG)(box->bo_Y1 - 1));
  40.    Draw(rp, (LONG)(box->bo_X2 + 1), (LONG)(box->bo_Y1 - 1));
  41.    Draw(rp, (LONG)(box->bo_X2 + 1), (LONG)(box->bo_Y2 + 1));
  42.    Draw(rp, (LONG)(box->bo_X1 - 1), (LONG)(box->bo_Y2 + 1));
  43.    Draw(rp, (LONG)(box->bo_X1 - 1), (LONG)(box->bo_Y1 - 1));
  44. }
  45.     /* Draw box in given window with offset */
  46.  
  47.    VOID
  48. draw_box_with_offset(struct Window  *win, struct Box  *box, SHORT xoffset,
  49.                                   SHORT yoffset)
  50. {
  51.    struct RastPort  *rp = win->RPort;
  52.  
  53.    SetDrMd(rp, (LONG)COMPLEMENT);
  54.    Move(rp, (LONG)(box->bo_X1 + xoffset), (LONG)(box->bo_Y1 + yoffset));
  55.    Draw(rp, (LONG)(box->bo_X2 + xoffset), (LONG)(box->bo_Y1 + yoffset));
  56.    Draw(rp, (LONG)(box->bo_X2 + xoffset), (LONG)(box->bo_Y2 + yoffset));
  57.    Draw(rp, (LONG)(box->bo_X1 + xoffset), (LONG)(box->bo_Y2 + yoffset));
  58.    Draw(rp, (LONG)(box->bo_X1 + xoffset), (LONG)(box->bo_Y1 + yoffset));
  59. }
  60.     /* Get head of EXEC list */
  61.  
  62.    VOID *
  63. get_head(struct List  *list)
  64. {
  65.    struct Node  *node = list->lh_Head;
  66.  
  67.    /* empty list ? */
  68.    if (!node->ln_Succ) {
  69.       node = NULL;
  70.    }
  71.    return((VOID *)node);
  72. }
  73.     /* Get tail of EXEC list */
  74.  
  75.    VOID *
  76. get_tail(struct List  *list)
  77. {
  78.    struct Node  *node = list->lh_TailPred;
  79.  
  80.    /* empty list ? */
  81.    if (!node->ln_Pred) {
  82.       node = NULL;
  83.    }
  84.    return((VOID *)node);
  85. }
  86.     /* Get next node of EXEC list entry */
  87.  
  88.    VOID *
  89. get_succ(struct Node  *node)
  90. {
  91.    node = node->ln_Succ;
  92.    /* end of list ? */
  93.    if (!node->ln_Succ) {
  94.       node = NULL;
  95.    }
  96.    return((VOID *)node);
  97. }
  98.     /* Get previous node of EXEC list entry */
  99.  
  100.    VOID *
  101. get_pred(struct Node  *node)
  102. {
  103.    node = node->ln_Pred;
  104.    /* start of list ? */
  105.    if (!node->ln_Pred) {
  106.       node = NULL;
  107.    }
  108.    return((VOID *)node);
  109. }
  110.     /* Get node from list by given number */
  111.  
  112.    VOID *
  113. get_node(struct List  *list, USHORT num)
  114. {
  115.    struct Node  *node;
  116.  
  117.    if (node = get_head(list)) {
  118.       while (num-- && (node = get_succ(node)));
  119.    }
  120.    return((VOID *)node);
  121. }
  122.     /* Duplicate text string */
  123.  
  124.    SHORT
  125. duplicate_string(BYTE **ptr, BYTE *text)
  126. {
  127.    LONG len;
  128.    SHORT status = EDITOR_STATUS_NORMAL;
  129.  
  130.    if (!text || (len = strlen(text) + 1) == 1) {
  131.       *ptr = NULL;
  132.    } else {
  133.       if (!(*ptr = DosAllocMem(len))) {
  134.      status = EDITOR_ERROR_OUT_OF_MEM;
  135.       } else {
  136.      CopyMem(text, *ptr, len);
  137.       }
  138.    }
  139.    return(status);
  140. }
  141.     /* Duplicate text list */
  142.  
  143.    SHORT
  144. duplicate_text_list(struct Template  *old_tp, struct Template  *new_tp)
  145. {
  146.    struct Node  *node;
  147.    SHORT status = EDITOR_STATUS_NORMAL;
  148.  
  149.    NewList(&new_tp->tp_TextList);
  150.    for (node = get_head(&old_tp->tp_TextList); node &&
  151.             status == EDITOR_STATUS_NORMAL; node = get_succ(node)) {
  152.       status = add_template_text_list_entry(new_tp, node->ln_Name);
  153.    }
  154.    return(status);
  155. }
  156.     /* Build template text list from given text array */
  157.  
  158.    SHORT
  159. build_template_text_list(struct Template  *tp, BYTE **text_array)
  160. {
  161.    BYTE  *text;
  162.    SHORT status = EDITOR_STATUS_NORMAL;
  163.  
  164.    NewList(&tp->tp_TextList);
  165.    for (text = *text_array; text && status == EDITOR_STATUS_NORMAL;
  166.                              text = *++text_array) {
  167.       status = add_template_text_list_entry(tp, text);
  168.    }
  169.    if (status != EDITOR_STATUS_NORMAL) {
  170.       free_template_text_list(tp);
  171.    }
  172.    return(status);
  173. }
  174.     /* Build text list entry */
  175.  
  176.    struct Node *
  177. build_text_list_entry(BYTE *text)
  178. {
  179.    struct Node  *node;
  180.  
  181.    if (node = AllocMem((LONG)sizeof(struct Node), (LONG)MEMF_PUBLIC |
  182.                                   MEMF_CLEAR)) {
  183.       if (!(node->ln_Name = DosAllocMem((LONG)(strlen(text) + 1)))) {
  184.      FreeMem(node, (LONG)sizeof(struct Node));
  185.      node = NULL;
  186.       } else {
  187.      strcpy(node->ln_Name, text);
  188.       }
  189.    }
  190.    return(node);
  191. }
  192.     /* Add text entry to template text list */
  193.  
  194.    SHORT
  195. add_template_text_list_entry(struct Template  *tp, BYTE *text)
  196. {
  197.    struct Node  *node;
  198.    SHORT status = EDITOR_STATUS_NORMAL;
  199.  
  200.    if (!(node = build_text_list_entry(text))) {
  201.       status = EDITOR_ERROR_OUT_OF_MEM;
  202.    } else {
  203.       AddTail(&tp->tp_TextList, node);
  204.    }
  205.    return(status);
  206. }
  207.     /* Free template text list */
  208.  
  209.    VOID
  210. free_template_text_list(struct Template  *tp)
  211. {
  212.    struct List  *list = &tp->tp_TextList;
  213.    struct Node  *node;
  214.  
  215.    while (node = RemTail(list)) {
  216.       free_text_list_entry(node);
  217.    }
  218. }
  219.     /* Free text list entry */
  220.  
  221.    VOID
  222. free_text_list_entry(struct Node  *node)
  223. {
  224.    DosFreeMem(node->ln_Name);
  225.    FreeMem(node, (LONG)sizeof(struct Node));
  226. }
  227.     /* Build template text array from template text list */
  228.  
  229.    SHORT
  230. build_template_text_array(struct Template  *tp)
  231. {
  232.    struct GadgetData  *gd = &tp->tp_Data.tp_GadgetData;
  233.    struct List        *list = &tp->tp_TextList;
  234.    struct Node        *node;
  235.    BYTE   **text_array;
  236.    USHORT count = 0;
  237.    SHORT  status = EDITOR_STATUS_NORMAL;
  238.  
  239.    /* Count entries in text list */
  240.    for (node = get_head(list); node; node = get_succ(node)) {
  241.       count++;
  242.    }
  243.    if (!(text_array = DosAllocMem((LONG)(count + 1) * sizeof(BYTE *)))) {
  244.       status = EDITOR_ERROR_OUT_OF_MEM;
  245.    } else {
  246.       switch (tp->tp_Type) {
  247.      case TEMPLATE_TYPE_MX :
  248.         gd->gd_SpecialData.gd_MXData.gd_MXTextArray = text_array;
  249.         break;
  250.      case TEMPLATE_TYPE_CYCLE :
  251.         gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray = text_array;
  252.         break;
  253.       }
  254.  
  255.       /* Fill text array with pointers to text list entries */
  256.       for (node = get_head(list); node && status == EDITOR_STATUS_NORMAL;
  257.                             node = get_succ(node)) {
  258.      *text_array++ = node->ln_Name;
  259.       }
  260.       *text_array = NULL;   /* mark end of text array */
  261.    }
  262.    return(status);
  263. }
  264.     /* Free template text array */
  265.  
  266.    VOID
  267. free_template_text_array(struct Template  *tp)
  268. {
  269.    struct GadgetData  *gd = &tp->tp_Data.tp_GadgetData;
  270.    BYTE **text_array;
  271.  
  272.    switch (tp->tp_Type) {
  273.       case TEMPLATE_TYPE_MX :
  274.      text_array = gd->gd_SpecialData.gd_MXData.gd_MXTextArray;
  275.      gd->gd_SpecialData.gd_MXData.gd_MXTextArray = NULL;
  276.      break;
  277.       case TEMPLATE_TYPE_CYCLE :
  278.      text_array = gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray;
  279.      gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray = NULL;
  280.      break;
  281.       default :
  282.      text_array = NULL;
  283.      break;
  284.    }
  285.    DosFreeMem(text_array);
  286. }
  287.     /* Open font with given attribute */
  288.  
  289.    struct TextFont *
  290. open_font(struct TextAttr  *ta)
  291. {
  292.    struct TextFont  *tf;
  293.  
  294.    tf = OpenFont(ta);
  295.    if (!tf || tf->tf_YSize != ta->ta_YSize) {
  296.       tf = OpenDiskFont(ta);
  297.    }
  298.    return(tf);
  299. }
  300.     /* Close font */
  301.  
  302.    VOID
  303. close_font(struct TextFont  *tf)
  304. {
  305.    if (tf) {
  306.       CloseFont(tf);
  307.    }
  308. }
  309.